<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions" @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan" collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.storeGoodsId" placeholder="商铺商品编号" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.barCode" placeholder="条形码" />
          </el-form-item>
          <el-form-item>
            <el-select v-model="query.params.barCodeType" placeholder="条型码类型">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.storeId" placeholder="商铺编号" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增组合商品</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="商铺商品编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.storeGoodsId }}
          </template>
        </el-table-column>
        <el-table-column label="租户编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.tenantId }}
          </template>
        </el-table-column>
        <el-table-column label="组合商品编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.parentId }}
          </template>
        </el-table-column>
        <el-table-column fixed="left" label="条形码" width="210">
          <template slot-scope="scope">
            <img :id="'image'+scope.row.storeGoodsId" />
          </template>
        </el-table-column>
        <el-table-column label="条型码类型" width="110">
          <template slot-scope="scope">
            {{ scope.row.barCodeType }}
          </template>
        </el-table-column>
        <el-table-column label="组织编号" width="110">
          <template slot-scope="scope">
            {{ scope.row.organId }}
          </template>
        </el-table-column>
        <el-table-column label="组织路径" width="110">
          <template slot-scope="scope">
            {{ scope.row.organPath }}
          </template>
        </el-table-column>
        <el-table-column label="价格" width="110">
          <template slot-scope="scope">
            {{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column label="进货价" width="110">
          <template slot-scope="scope">
            {{ scope.row.originPrice }}
          </template>
        </el-table-column>
        <el-table-column label="折扣价" width="110">
          <template slot-scope="scope">
            {{ scope.row.discountPrice }}
          </template>
        </el-table-column>
        <el-table-column label="建议售价" width="110">
          <template slot-scope="scope">
            {{ scope.row.suggestedRetailPrice }}
          </template>
        </el-table-column>
        <el-table-column label="成本价" width="110">
          <template slot-scope="scope">
            {{ scope.row.costPrice }}
          </template>
        </el-table-column>
        <el-table-column label="库存量">
          <template slot-scope="scope">
            {{ scope.row.storeCount }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="操作" width="200">
          <template slot-scope="scope">
            <el-link type="warning" icon="el-icon-edit" size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-link>
            <el-link type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-link>
            <el-link icon="el-icon-edit" type="primary" style="cursor: pointer;" @click="handleShowGoodsAdd(scope.$index, scope.row)">
              维护库存
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="StoreGoodsBarcodeRules" ref="StoreGoodsBarcodeForm">
          <el-form-item v-if="addForVisible" label="编号">
            <el-input v-model="form.id" disabled placeholder="编号" auto-complete="off" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps" :options="tenantOptions" @change="chooseTenantForm" style="width:100%" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织" :props="props" :options="organFormOptions" @change="chooseOrganForm" style="width:100%" collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item label="商铺商品编号" required prop="storeGoodsId">
            <el-input v-model="form.storeGoodsId" placeholder="商铺商品编号" auto-complete="off" @focus="handleShowStoreGoodsList" suffix-icon="el-icon-goods" />
          </el-form-item>
          <el-form-item label="条形码" required prop="barCode">
            <el-input v-model="form.barCode" placeholder="条形码" auto-complete="off" />
          </el-form-item>
          <el-form-item label="条型码类型" required prop="barCodeType">
            <el-select v-model="form.barCodeType" placeholder="1:code39, 2:code128" style="width:100%">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商铺编号" required prop="storeId">
            <el-input v-model="form.storeId" placeholder="商铺编号" auto-complete="off" @focus="handleShowStoreList" suffix-icon="el-icon-s-shop" />
          </el-form-item>
          <el-form-item label="单价" required prop="price">
            <el-input-number size="mini" v-model="form.price" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="进货价" required prop="originPrice">
            <el-input-number size="mini" v-model="form.originPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="折扣价" required prop="discountPrice">
            <el-input-number size="mini" v-model="form.discountPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="建议售价" required prop="suggestedRetailPrice">
            <el-input-number size="mini" v-model="form.suggestedRetailPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="成本价" required prop="costPrice">
            <el-input-number size="mini" v-model="form.costPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="库存量" required prop="storeCount">
            <el-input-number size="mini" v-model="form.storeCount" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="标注">
            <el-input v-model="form.remark" placeholder="标注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogCompositeFormVisible">
        <el-form :model="form" :rules="StoreGoodsBarcodeRules" ref="StoreGoodsBarcodeForm">
          <el-form-item v-if="addForVisible" label="编号">
            <el-input v-model="form.id" disabled placeholder="编号" auto-complete="off" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps" :options="tenantOptions" @change="chooseTenantForm" style="width:100%" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织" :props="props" :options="organFormOptions" @change="chooseOrganForm" style="width:100%" collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item label="商铺商品编号" required prop="storeGoodsId">
            <el-input v-model="form.storeGoodsId" placeholder="商铺商品编号" auto-complete="off" @focus="handleShowStoreGoodsList" suffix-icon="el-icon-goods" />
          </el-form-item>
          <el-form-item label="条形码" required prop="barCode">
            <el-input v-model="form.barCode" placeholder="条形码" auto-complete="off" />
          </el-form-item>
          <el-form-item label="条型码类型" required prop="barCodeType">
            <el-select v-model="form.barCodeType" placeholder="1:code39, 2:code128" style="width:100%">
              <el-option v-for="item in query.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商铺编号" required prop="storeId">
            <el-input v-model="form.storeId" placeholder="商铺编号" auto-complete="off" @focus="handleShowStoreList" suffix-icon="el-icon-s-shop" />
          </el-form-item>
          <el-form-item label="单价" required prop="price">
            <el-input-number size="mini" v-model="form.price" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="进货价" required prop="originPrice">
            <el-input-number size="mini" v-model="form.originPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="折扣价" required prop="discountPrice">
            <el-input-number size="mini" v-model="form.discountPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="建议售价" required prop="suggestedRetailPrice">
            <el-input-number size="mini" v-model="form.suggestedRetailPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="成本价" required prop="costPrice">
            <el-input-number size="mini" v-model="form.costPrice" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="库存量" required prop="storeCount">
            <el-input-number size="mini" v-model="form.storeCount" style="width:100%"></el-input-number>
          </el-form-item>
          <el-form-item label="标注">
            <el-input v-model="form.remark" placeholder="标注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="secondTitleMap[secondDialogStatus]" :visible.sync="showStoreListVisiable">
        <el-table v-loading="listLoading" :data="storeList" element-loading-text="Loading" border fit highlight-current-row>
          <el-table-column align="center" label="选择" width="55">
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="storeId" @change="storeSelectedChange(scope.row)"></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="店铺名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.storeName }}
            </template>
          </el-table-column>
          <el-table-column label="所属组织" width="110">
            <template slot-scope="scope">
              {{ scope.row.organId }}
            </template>
          </el-table-column>
          <el-table-column label="纬度" width="110">
            <template slot-scope="scope">
              {{ scope.row.latitude }}
            </template>
          </el-table-column>
          <el-table-column label="经度" width="110">
            <template slot-scope="scope">
              {{ scope.row.longitude }}
            </template>
          </el-table-column>
          <el-table-column label="标注">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="showStoreListVisiable = false">取 消</el-button>
          <el-button type="primary" @click="handleGetSelectedStore">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="secondTitleMap[secondDialogStatus]" :visible.sync="showStoreGoodsListVisiable">
        <el-table ref="storeGoodsTable" selection-change="handleSelectionChange" v-loading="listLoading" :data="storeGoodsList" element-loading-text="Loading" border fit highlight-current-row>
          <el-table-column align="center" label="选择" width="40">
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="storeGoodsId" @change="storeGoodsSelectedChange(scope.row)">1</el-radio>
            </template>
          </el-table-column>
          <el-table-column label="商铺编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.storeId }}
            </template>
          </el-table-column>
          <el-table-column label="商品图片" width="110">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 100px" :src="scope.row.goodsImg">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.goodsName }}
            </template>
          </el-table-column>
          <el-table-column label="商品编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.goodsId }}
            </template>
          </el-table-column>
          <el-table-column label="商铺商品编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.id }}
            </template>
          </el-table-column>
          <el-table-column label="备注">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="showStoreGoodsListVisiable = false">取 消</el-button>
          <el-button type="primary" @click="handleGetSelectedStoreGoods">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <div class="block">
      <!--弹框-->
      <el-dialog :title="secondTitleMap[secondDialogStatus]" :visible.sync="showStoreCompositeGoodsListVisiable">
        <el-table ref="storeGoodsTable" selection-change="handleSelectionChange" v-loading="listLoading" :data="storeGoodsList" element-loading-text="Loading" border fit highlight-current-row>
          <el-table-column align="center" label="选择" width="55">
            <template slot-scope="scope">
              <el-radio :label="scope.row.id" v-model="storeGoodsId" @change="storeGoodsSelectedChange(scope.row)"></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="商铺编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.storeId }}
            </template>
          </el-table-column>
          <el-table-column label="商品图片" width="110">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 100px" :src="scope.row.goodsImg">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.goodsName }}
            </template>
          </el-table-column>
          <el-table-column label="商品编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.goodsId }}
            </template>
          </el-table-column>
          <el-table-column label="商铺商品编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.id }}
            </template>
          </el-table-column>
          <el-table-column label="备注">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button @click="showStoreGoodsListVisiable = false">取 消</el-button>
          <el-button type="primary" @click="handleGetSelectedStoreGoods">确 定</el-button>
        </div>
      </el-dialog>
    </div>


  </div>
</template>
<script>
import { getList, doDelete, doEdit } from '@/api/storeGoodsBarcode/'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode } from '@/api/utils'
import { getList as queryStoreGoodsList } from '@/api/StoreGoods/'
import { getList as queryStoreList } from '@/api/Store/'
import { getDictionaryGroup } from '@/api/dictionary'
import { getList as getOrganPager, querySubordinateOrgan, queryAssigneeOrgan } from '@/api/Organ'
import { getTenantList, getOrganList, getDictionary } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'
import JsBarcode from 'jsbarcode'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      showStoreListVisiable: false,
      showStoreGoodsListVisiable: false,
      dialogCompositeFormVisible: false,
      showStoreCompositeGoodsListVisiable: false,
      organTree: [],
      storeGoodsList: [],
      storeList: [],
      storeGoods: null,
      storeGoodsId: null,
      storeId: null,
      direction: 'rtl',
      form: {
        id: '',
        storeGoodsId: '',
        tenantId: '',
        parentId: '',
        isDeleted: '',
        barCode: '',
        barCodeType: '',
        organId: '',
        organPath: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        storeId: '',
        updateDate: '',
        remark: '',
        price: '',
        originPrice: '',
        discountPrice: '',
        suggestedRetailPrice: '',
        costPrice: '',
        storeCount: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑',
        addStoreGoods: '关联商铺商品',
        addStore: '关联商铺'
      },
      secondTitleMap: {
        addStoreGoods: '关联商铺商品',
        addStore: '关联商铺'
      },
      StoreGoodsBarcodeRules: {},
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organTreeProps: {
        children: 'children',
        label: 'organName',
        url: 'url'
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organFormOptions: [],
      tenantOptions: [],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      // 新增和编辑弹框标题
      dialogStatus: '',
      secondDialogStatus: '',
      query: {
        params: {
          id: '', //,
          storeGoodsId: '', //商铺商品编号,
          tenantId: '', //租户编号,
          parentId: '', //,
          isDeleted: '', //,
          barCode: '', //条形码,
          barCodeType: '', //1:code39, 2:code128,    
          organId: '', //组织编号,                
          organPath: '', //组织路径,         
          createBy: '', //生成者,
          createDate: '', //生成时间,
          updateBy: '', //更新者,
          storeId: '', //商铺编号,
          goodsId: '', // 商品编号
          updateDate: '', //更新时间,
          remark: '', //,
          price: '', //价格,
          originPrice: '', //进货价,
          discountPrice: '', //折扣价,
          suggestedRetailPrice: '', //建议售价,
          costPrice: '', //成本价
        },
        storeGoods: {
          id: '',
          parentId: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: '',
          organId: '',
          organPath: '',
          isDeleted: '',
          storeId: '',
          goodsId: '',
          goodsImg: '',
          goodsName: '',
          goodsPrice: '',
          discountPrice: '',
          isHot: '',
          costPrice: '',
          suggestedRetailPrice: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        store: {
          addressId: '',
          createBy: '',
          createDate: '',
          enable: '',
          id: '',
          latitude: '',
          longitude: '',
          organId: '',
          organPath: '',
          remark: '',
          storeName: '',
          updateBy: '',
          updateDate: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        timeValue: '',
        options: [{
          value: '1',
          label: 'code39'
        }, {
          value: '2',
          label: 'code128'
        }],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.initOrganFormTree()
    this.handleFiltTenant()
    this.fetchData()
  },
  watch: {
    list: 'createBarcode'
  },

  methods: {
    fetchData() {
      this.listLoading = true
      this.createOrganQuery()
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        if (!data.entities || typeof data.successful != 'undefined') {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
          return;
        }
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })


    },
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        }

      }
      this.query.params.organId = this.query.params.organId || getOrganId()
      this.query.params.organPath = this.query.params.organPath || getOrganPath()
    },
    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function(item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function(element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      debugger
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function(element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function() {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function() {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(nodes) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      this.form.tenantId = null
      let that = this
      debugger
      if (nodesInfo.length) {
        this.form.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function() {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function() {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(nodes) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.form.organPath = nodesInfo[0].data.organPath
        this.form.organId = nodesInfo[0].data.id
        this.form.tenantId = nodesInfo[0].data.tenantId
      }
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function(item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'
      this.form.id = ''
      this.form.storeGoodsId = ''
      this.form.tenantId = getTenantId()
      this.form.parentId = ''
      this.form.isDeleted = ''
      this.form.barCode = ''
      this.form.barCodeType = ''
      this.form.organId = getOrganId()
      this.form.organPath = getOrganPath()
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = ''
      this.form.updateBy = getCurrentLoginUserId()
      this.form.storeId = ''
      this.form.updateDate = ''
      this.form.remark = ''
      this.form.price = ''
      this.form.originPrice = ''
      this.form.discountPrice = ''
      this.form.suggestedRetailPrice = ''
      this.form.costPrice = ''
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)
      this.form.id = row.id
      this.form.storeGoodsId = row.storeGoodsId
      this.form.tenantId = row.tenantId
      this.form.parentId = row.parentId
      this.form.isDeleted = row.isDeleted
      this.form.barCode = row.barCode
      this.form.barCodeType = row.barCodeType
      this.form.organId = row.organId
      this.form.organPath = row.organPath
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.updateBy = row.updateBy
      this.form.storeId = row.storeId
      this.form.updateDate = row.updateDate
      this.form.remark = row.remark
      this.form.price = row.price
      this.form.originPrice = row.originPrice
      this.form.discountPrice = row.discountPrice
      this.form.suggestedRetailPrice = row.suggestedRetailPrice
      this.form.costPrice = row.costPrice
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleDelete(index, row) {
      console.log(index)
      console.log(row)

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const data = response.data
          console.log(data)
          if (data.status == 1) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else if (data.status == 403) {
            this.$message({
              type: 'warning',
              message: data.description
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger

      doEdit(this.form).then(response => {
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      this.query.params.organId = nodesInfo[0].data.id
    },
    handleShowStoreGoodsList(index, row) {
      this.listLoading = true
      this.showStoreGoodsListVisiable = true
      this.secondDialogStatus = 'addStoreGoods'
      this.query.storeGoods.tenantId = this.form.tenantId || row.tenantId || getTenantId()
      this.query.storeGoods.organId = this.form.organId || row.organId || getOrganId()
      this.query.storeGoods.storeId = this.form.storeId || row.storeId

      queryStoreGoodsList(this.query.storeGoods, this.query.storeGoods.currentPage, this.query.storeGoods.pageSize).then(response => {
        const data = response.data
        this.query.storeGoods.total = data.totalCount
        this.query.storeGoods.pageSize = data.pageSize
        this.query.storeGoods.currentPage = data.currentPage
        this.storeGoodsList = data.entities
        this.listLoading = false
      })
    },
    handleGetSelectedStoreGoods() {
      this.$confirm('您确定关联所选商铺商品吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.form.storeGoodsId = this.storeGoods.id
        this.form.price = this.storeGoods.goodsPrice
        this.form.originPrice = this.storeGoods.costPrice
        this.form.discountPrice = this.storeGoods.discountPrice
        this.form.suggestedRetailPrice = this.storeGoods.suggestedRetailPrice
        this.form.costPrice = this.storeGoods.costPrice
        this.form.goodsId =  this.storeGoods.goodsId
        this.showStoreGoodsListVisiable = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作已取消，请重新操作！'
        })
      })
    },
    handleShowStoreList(index, row) {
      this.showStoreListVisiable = true
      this.listLoading = true

      this.secondDialogStatus = 'addStore'
      this.query.store.tenantId = this.form.tenantId
      this.query.store.organId = this.form.organId
      this.query.store.storeId = this.form.storeId
      queryStoreList(this.query.store, this.query.store.currentPage, this.query.store.pageSize).then(response => {
        const data = response.data
        this.query.store.total = data.totalCount
        this.query.store.pageSize = data.pageSize
        this.query.store.currentPage = data.currentPage
        this.storeList = data.entities
        this.listLoading = false
      })

    },
    storeGoodsSelectedChange(row) {
      this.storeGoods = row
    },
    storeSelectedChange(row) {
      this.store = row

    },
    handleGetSelectedStore() {
      this.$confirm('您确定关联所选商铺吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
        this.form.storeId = this.store.id
        this.showStoreListVisiable = false
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作已取消，请重新操作！'
        })
      })
    },
    handleSelectionChange() {

    },
    handleEditCompositeGoods() {
      this.dialogCompositeFormVisible = true;
    },
    createBarcode() {
      this.list.forEach(function(goodsBarcode) {
        setTimeout(function() {
          JsBarcode('#image' + goodsBarcode.storeGoodsId, goodsBarcode.barCode, {
            format: goodsBarcode.barCodeType || "ean13", //选择要使用的条形码类型
            width: 1, //设置条之间的宽度
            height: 25, //高度
            displayValue: true, //是否在条形码下方显示文字
            textAlign: "center", //设置文本的水平对齐方式
            textPosition: "bottom", //设置文本的垂直位置
            textMargin: 5, //设置条形码和文本之间的间距
            fontSize: 15, //设置文本的大小
            background: "#eee", //设置条形码的背景
            lineColor: "#2196f3", //设置条和文本的颜色。
            margin: 15 //设置条形码周围的空白边距
          })

        }, 300)
      })

    }

  }
}

</script>
<style scoped>
.el-radio {
  margin-left: 6px;
}
.el-radio .el-radio__label {
  display: none;
}

</style>
